﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using WebApiV2.Data;
using WebApiV2.Data.Entity;
using WebApiV2.Domain;

namespace WebApiV2.Until
{
    public class DbIntializer
    {
        public static void Seed(IServiceProvider serviceProvider)
        {
            using (var scope = serviceProvider.CreateScope())
            {
                var db = scope.ServiceProvider.GetService(typeof(Admain3200DbContext)) as Admain3200DbContext;

                var _user = scope.ServiceProvider.GetService(typeof(IRespository<Users>)) as EfResponsitory<Users>;

                var _role= scope.ServiceProvider.GetService(typeof(IRespository<Roles>)) as EfResponsitory<Roles>;

                db.Database.EnsureCreated();

                
                var hasUser = db.Users.Any();

                if (!hasUser)
                {
                    var role = new Roles()
                    {
                        RoleName = "饲养员"
                    };
                    _role.Insert(role);

                    var user = new Users()
                    {
                        UserName = "饲养员1号",
                        PassWord = "admin",
                        Roles = role
                    };
                    _user.Insert(user);
                }
            }
        }

    }
}
